package com.xscr.api.controller;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.csource.common.MyException;
import org.csource.upload.UploadFileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.rop.response.Response;
import com.xscr.api.service.user.UserAction;
import com.xscr.forum.po.ForumImage;
import com.xscr.forum.po.ForumTopic;
import com.xscr.forum.service.ForumImageService;
import com.xscr.forum.service.ForumTopicService;

@Controller
@RequestMapping("/topic")
public class TopicController {
	private static final Logger logger=LoggerFactory.getLogger(TopicController.class);
	
	@Autowired
	private ForumTopicService forumTopicService;
	
	@Autowired
	private ForumImageService forumImageService;
	
	/**
	 * 发帖
	 * @param request
	 * @param photo
	 * @param userId
	 * @param communityId
	 * @param title
	 * @param content
	 * @param anonymous
	 * @return
	 * @throws IOException
	 * @throws Exception
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping(value = "/postTopic", method = RequestMethod.POST)
	public Response commentAdd(
			HttpServletRequest request,
			@RequestParam(value = "file", required = false) MultipartFile[] photo,
			@RequestParam(value = "userId", required = false) String userId,
			@RequestParam(value = "communityId", required = false) String communityId,
			@RequestParam(value = "title", required = false) String title,
			@RequestParam(value = "content", required = false) String content,
			@RequestParam(value = "anonymous", required = false) String anonymous)
			throws IOException, Exception, ParseException {
		Response response = new Response();		
		String name = null;		
		
		try {
	        ForumTopic forumTopic=new ForumTopic();
	        forumTopic.setContent(content);
	        forumTopic.setCircleId(Integer.parseInt(communityId));
	        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式  
	        forumTopic.setCreateTime(df.parse(df.format(new Date() )));
	        forumTopic.setStatus(1);
	        forumTopic.setTitle(title);
	        forumTopic.setUserId(Integer.parseInt(userId));
	        forumTopic.setDescription(anonymous);
	        Integer topicId= forumTopicService.saveForumTopic(forumTopic);
			
			if (topicId!=null&&photo != null) {
				for (MultipartFile f : photo) {
					name = f.getOriginalFilename();
					String imgUrl = UploadFileUtil.uploadFile(f.getInputStream(),
							"png", null);                 
	               ForumImage forumImage=new ForumImage();
	               forumImage.setStatus(1);
	               forumImage.setTopicId(topicId);
	               forumImage.setCreateTime(df.parse(df.format(new Date())));
	               forumImage.setUrl(imgUrl);
	               forumImageService.saveForumImage(forumImage);
				}
			}
		} catch (Exception e) {
			logger.error("postTopic error", e);
			response.getResult().setResult("0");
			response.getResult().setMessage("发帖错误");
			return response;
		}
		response.getResult().setResult("1");
		response.getResult().setMessage("发帖成功");
		return response;
	}
}
